﻿using System;
using System.Collections;
using System.Data;
using Model;
using Resources;
using Service;

public partial class sparepartOut : System.Web.UI.Page
{
    StockService _StockService = new StockService();
    protected void Page_Load(object sender, EventArgs e)
    {
        
        
        if(!IsPostBack)
        {
            lblType.Text = Request.QueryString["type"];
            lblSparepartNumber.Text = Request.QueryString["informationId"];
            lblModel.Text = Request.QueryString["model"];     
        }
    }
    protected void btnValidate_Click(object sender, EventArgs e)
    {
        if (!new Utility().TestPermissions(Resource.PermissionSparepartOut))
        {
            lblMessageValidate.Text = Resource.Permission;
            Page_Load(null, null);
            return;
        }
        else
        {
        if (!ValidateData())
        {
            return;
        }

        SparepartPurchaseInfo purchaseInfo = new SparepartPurchaseInfo();
        purchaseInfo = _StockService.GetPurchaseInformation(Int32.Parse(Request.QueryString["informationId"]));

        if (purchaseInfo.Quantity > purchaseInfo.NbOut)
        {

            purchaseInfo.NbOut = purchaseInfo.NbOut + 1;
            if (purchaseInfo.StartDate < DateTime.Parse(Resource.DateTimeNull))
                purchaseInfo.StartDate = DateTime.Parse(Resource.EmptylDateTime);

            if (purchaseInfo.EndDate < DateTime.Parse(Resource.DateTimeNull))
                purchaseInfo.EndDate = DateTime.Parse(Resource.EmptylDateTime);

            if (purchaseInfo.PurchaseDate < DateTime.Parse(Resource.DateTimeNull))
                purchaseInfo.PurchaseDate = DateTime.Parse(Resource.EmptylDateTime);

            _StockService.UpdatePurchaseInformation(purchaseInfo);
            SparepartComposeEquipment sparepartComposeEquipment = GetSparepartComposePurchaseInfo();
            _StockService.InsertSparepartComposeEquip(sparepartComposeEquipment);
            lblRequiredSoftware0.Text = "Spare-part has been attached to equipment successfully !";
        }
        else

            lblRequiredSoftware0.Text = "No more spare-part available in stock.";


        lblRequiredSoftware0.Visible = true;


    }
}
    private SparepartComposeEquipment GetSparepartComposePurchaseInfo()
    {
        SparepartComposeEquipment sparepartComposeEquipment = new SparepartComposeEquipment();
        sparepartComposeEquipment.CodeNumber =txtCode.Text;
        sparepartComposeEquipment.ComposeId = 0;
        sparepartComposeEquipment.DateCreated = DateTime.Parse(DateTime.Now.ToString(Resource.DateEnglishFormat));
        sparepartComposeEquipment.Description = txtDesc.Text;
        sparepartComposeEquipment.SparepartId = Int32.Parse(Request.QueryString["informationId"]);
        return sparepartComposeEquipment;
    }
    private bool ValidateData ()
    {
        if(String.IsNullOrEmpty(txtCode.Text))
        {
            lblRequiredSoftware.Text = Resource.MsgFillInformation;
            lblRequiredSoftware.Visible = true;
            return false;
        }
        lblRequiredSoftware.Visible = false;
        
        return true;
            
        
    }


    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("sparepartView.aspx");
    }
}
